package com.example.hyw.szysteam.view;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.myEmail.MackCode;
import com.example.hyw.szysteam.myEmail.SendMailUtil;
import com.example.hyw.szysteam.presenter.ResetPresenter;
import com.example.hyw.szysteam.utils.CodeTypeCheckUtil;
import com.example.hyw.szysteam.utils.ShowTips;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

/**
 * Created by HYW on 2018/5/19.
 */

public class ResetPswDialog extends DialogFragment {
    TextView title;
    Button sendCode;
    Button reset;
    EditText email;
    EditText psw;
    EditText Ecode;
    View view;
    String code;
    private ResetPresenter resetPresenter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.reset_registe,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private Handler uiHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    sendCode.setText(msg.arg1+"秒");
                    break;
                case 2:
                default:
                    sendCode.setText("获取验证码");
                    sendCode.setClickable(true);
            }
        }
    };

    public void initView(){
        reset=view.findViewById(R.id.button_regist);
        title=view.findViewById(R.id.rr_title);
        email =view.findViewById(R.id.email);
        psw=view.findViewById(R.id.password);
        Ecode=view.findViewById(R.id.code);
        sendCode=view.findViewById(R.id.send_code);
        resetPresenter=new ResetPresenter(this);
        title.setText("密码重置");
        reset.setText("重置");
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(CodeTypeCheckUtil.isEmail(email.getText().toString())){
                    if(CodeTypeCheckUtil.passwordCheck(psw.getText().toString())){
                        if(Ecode.getText().toString().equals(code)){
                            resetPresenter.toResetpsw(email.getText().toString(),psw.getText().toString());
                        }else{
                            Toast.makeText(getContext(),"验证码错误！",Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(getContext(),"密码应由6-16位的字母与数字组成！",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(getContext(),"请输入正确的邮箱地址！",Toast.LENGTH_SHORT).show();
                }
            }
        });
        sendCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                code=MackCode.mackCode();
                SendMailUtil.send("826664258@qq.com","【验证码】您本次的验证码为："+code+"。");
                sendCode.setClickable(false);
                sendCode.setText("60秒");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message msg;
                        try{
                            for(int i=59;i>0;i--){
                                msg=new Message();
                                Thread.sleep(1000);
                                msg.what=1;
                                msg.arg1=i;
                                uiHandler.sendMessage(msg);
                            }
                            Thread.sleep(1000);
                            msg=new Message();
                            msg.what=2;
                            uiHandler.sendMessage(msg);

                        }catch (InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

        email.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                code="";
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    public void ResetSuccess(){
        ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_SUCCESS,"重置成功",getView(),getContext());
        this.dismiss();
    }

    public void ResetFail(){
        ShowTips.show(QMUITipDialog.Builder.ICON_TYPE_FAIL,"没有该用户",getView(),getContext());
    }
}
